package com.wushi.lenovo.asia5b.app_ui.profile.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mvp.base.BaseMvpActivity;
import com.mvp.base.model.ResultModel;
import com.mvp.common.GlobalValue;
import com.mvp.utils.ToastUtil;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_config.UserRepository;
import com.wushi.lenovo.asia5b.app_di.APPComponentFactory;
import com.wushi.lenovo.asia5b.app_di.other.presenter.CodePresenter;
import com.wushi.lenovo.asia5b.app_di.profile.mvpview.FirstPayPassActivityPresenter;
import com.wushi.lenovo.asia5b.app_di.profile.mvpview.FirstPayPassActivityView;
import com.wushi.lenovo.asia5b.app_event.ChangeProfileEvent;
import com.wushi.lenovo.asia5b.app_model.other.UserInfo;
import com.wushi.lenovo.asia5b.app_model.profile.CodeModel;
import com.wushi.lenovo.asia5b.app_model.profile.bean.ChangeProfileType;
import com.wushi.lenovo.asia5b.app_utils.WToastHelper;
import com.wushi.lenovo.asia5b.app_widget.ErrorTextView;
import com.wushi.lenovo.asia5b.app_widget.pass.PayPwdEditText;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import timber.log.Timber;

/**
 * Created by zhangyuncai on 2019/3/1.
 */
public class FirstPayPassActivity extends BaseMvpActivity<
        FirstPayPassActivityView, FirstPayPassActivityPresenter> implements FirstPayPassActivityView {
    @BindView(R.id.paypwd_firstpaypass_new)
    PayPwdEditText paypwd_firstpaypass_new;
    @BindView(R.id.paypwd_firstpaypass_renew)
    PayPwdEditText paypwd_firstpaypass_renew;
    @BindView(R.id.tv_firstpaypass_renew_error)
    ErrorTextView tv_firstpaypass_renew_error;
    @BindView(R.id.et_firstpaypass_code)
    EditText et_firstpaypass_code;
    @BindView(R.id.tv_firstpaypass_getcode)
    TextView tv_firstpaypass_getcode;
    @BindView(R.id.tv_firstpaypass_code_error)
    ErrorTextView tv_firstpaypass_code_error;
    @BindView(R.id.ll_firstpaypass_codevessel)
    LinearLayout ll_firstpaypass_codevessel;
    @BindView(R.id.bt_firstpaypass_save)
    Button bt_firstpaypass_save;
    @BindView(R.id.bt_firstpaypass_cancle)
    Button bt_firstpaypass_cancle;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_firstpaypass;
    }

    @Override
    protected void constructComponent() {
        APPComponentFactory.getAppBaseComponent().inject(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();

        initData();

        initEvent();
    }

    @Override
    protected void initView() {
        paypwd_firstpaypass_new.initStyle(R.drawable.edit_num_bg, 6, 0.33f, R.color.common_text_black, R.color.common_text_black, 20);
        paypwd_firstpaypass_renew.initStyle(R.drawable.edit_num_bg, 6, 0.33f, R.color.common_text_black, R.color.common_text_black, 20);
    }

    @Override
    protected void initData() {

    }

    private void save() {
        boolean b = false;
        //new_payment_password
        String newPass = paypwd_firstpaypass_new.getPwdText();
        String newPass2 = paypwd_firstpaypass_renew.getPwdText();

        //code
        String code = et_firstpaypass_code.getText().toString().trim();

        if (TextUtils.isEmpty(newPass) || !TextUtils.equals(newPass, newPass2)) {
            tv_firstpaypass_renew_error.setVisibility(View.VISIBLE);
            b = true;
        }
        if (TextUtils.isEmpty(code)) {
            b = true;
            tv_firstpaypass_code_error.setText(R.string.filed_is_empty);
            tv_firstpaypass_code_error.setVisibility(View.VISIBLE);
        } else {
            Object tag = et_firstpaypass_code.getTag();
            if (tag != null && tag instanceof String) {
                String tagCode = (String) tag;
                if (!TextUtils.equals(tagCode, code)) {
                    tv_firstpaypass_code_error.setText(R.string.invalid_code);
                    tv_firstpaypass_code_error.setVisibility(View.VISIBLE);
                    b = true;
                }
            }
        }

        if (b) {
            return;
        }
        getPresenter().changePass(newPass2, code);

    }

    @Override
    protected void initEvent() {
        tv_firstpaypass_code_error.setBindEditText(et_firstpaypass_code);
        paypwd_firstpaypass_new.setOnTextChangeListener(content -> {
            tv_firstpaypass_renew_error.setVisibility(View.GONE);
        });
        paypwd_firstpaypass_renew.setOnTextChangeListener(content -> {
            tv_firstpaypass_renew_error.setVisibility(View.GONE);
        });
    }

    @OnClick({
            R.id.bt_firstpaypass_cancle,
            R.id.bt_firstpaypass_save,
            R.id.tv_firstpaypass_getcode
    })
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_firstpaypass_cancle:
                finish();
                break;
            case R.id.bt_firstpaypass_save:
                save();
                break;
            case R.id.tv_firstpaypass_getcode:
                et_firstpaypass_code.setTag("");
                getPresenter().sendCode(CodePresenter.CodeType.CHANGE_PAYMENTPASS);
                break;
            default:
                break;
        }
    }

    @Override
    public void changePassResult(ResultModel resultModel) {
        if (resultModel.isSuccess()) {
            UserInfo userInfo = UserInfo.getUserInfo();
            userInfo.setHas_pay_pass("1");
            UserRepository.setUserInfo(userInfo);
            WToastHelper.showContentView(this, R.string.changepaypass_success);
            EventBus.getDefault().post(new ChangeProfileEvent(ChangeProfileType.ChangePayPass));
            finish();
        }
    }

    @Override
    public void sendCodeResult(ResultModel<CodeModel> resultModel, CodePresenter.CodeType codeType) {
        if (resultModel != null && resultModel.isSuccess()) {
            if (codeType == CodePresenter.CodeType.CHANGE_PAYMENTPASS) {
                ToastUtil.showToast(getString(R.string.otp_send_success));
                getPresenter().interval();
                CodeModel codeModel = resultModel.getData();
                if (codeModel != null) {
                    et_firstpaypass_code.setTag(codeModel.getOTP_code());
                    if (GlobalValue.isDebug) {
                        et_firstpaypass_code.setText(codeModel.getOTP_code());
                    }
                }
            }
        }
    }

    @Override
    public void intervalResult(long aLong) {
        Timber.d("--->>>>>>>>---aLong:" + aLong);
        if (aLong == 0) {
            tv_firstpaypass_getcode.setEnabled(true);
            tv_firstpaypass_getcode.setText(R.string.chongxinhuoquyanzhengma);
        } else {
            tv_firstpaypass_getcode.setEnabled(false);
            tv_firstpaypass_getcode.setText(aLong + "s");
        }
    }
}
